package com.douglas.managesystem.service.admin.impl;

import com.douglas.managesystem.common.NumberEnum;
import com.douglas.managesystem.common.RoleTypeEnum;
import com.douglas.managesystem.common.SearchEntity;
import com.douglas.managesystem.common.UUIDUtils;
import com.douglas.managesystem.dao.UserMapper;
import com.douglas.managesystem.dao.student.StudentMapper;
import com.douglas.managesystem.dao.student.TeacherMapper;
import com.douglas.managesystem.entity.User;
import com.douglas.managesystem.entity.studentpojo.Student;
import com.douglas.managesystem.entity.studentpojo.Teacher;
import com.douglas.managesystem.service.admin.UserService;
import com.douglas.managesystem.vo.UserVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Description TODO
 * @Author 以梦为馬 <Douglas1314@126.com>
 * @Version V1.0.0
 * @Date 2020-10-24 0:07
 **/
@SuppressWarnings("all")
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    /**
     * 学生数据
     */
    @Autowired
    private StudentMapper studentMapper;

    /**
     * 教师数据
     */
    @Autowired
    private TeacherMapper teacherMapper;
    /**
     * 1.用户登录
     * @param user
     * @return
     */
    @Override
    public UserVO loginUser(UserVO user) {
        UserVO userVO = new UserVO();
        // 学生
        if(RoleTypeEnum.STUDENT.getCode().equals(user.getUserRole().toString())){
            Student student = studentMapper.queryStuByStuNumber(user.getUsername());
             if(student == null){
                 userVO.setVcode(NumberEnum.ZERO.getCode());
                 return userVO;
             }
            // 2.1用户密码加密的盐
            ByteSource salt = ByteSource.Util.bytes(student.getStuNumber() + user.getPassword());
            // 2.2加盐后的密码
            SimpleHash saltPassword = new SimpleHash("MD5", user.getPassword(), salt, 1024);
            if(!student.getPassword().equals(saltPassword.toHex())){
                userVO.setVcode(NumberEnum.ZERO.getCode());
                return userVO;
            }
            //3.成功返回用户信息
            userVO.setUsername(student.getName());
            userVO.setPassword(student.getPassword());
            userVO.setRealname(student.getEngName());
            userVO.setUserRole(new Integer(RoleTypeEnum.STUDENT.getCode()));
//            userVO.setStatus(userDb.getStatus());
            userVO.setId(student.getStuNumber());
        }else if(RoleTypeEnum.TEACHER.getCode().equals(user.getUserRole().toString())){
        // 老师
            Teacher teacher = teacherMapper.queryTeacherByTeacherId(user.getUsername());
            if(teacher == null){
                userVO.setVcode(NumberEnum.ZERO.getCode());
                return userVO;
            }
            // 如果用户状态为禁用 返回 提示用户账号已禁用 请联系管理员
            if(teacher.getStatus().toString().equals(NumberEnum.ZERO.getCode())){
                userVO.setStatus(new Integer(teacher.getStatus()));
                return userVO;
            }
            // 2.1用户密码加密的盐
            ByteSource salt = ByteSource.Util.bytes(teacher.getTeacherId() + user.getPassword());
            // 2.2加盐后的密码
            SimpleHash saltPassword = new SimpleHash("MD5", user.getPassword(), salt, 1024);
            if(!teacher.getPassword().equals(saltPassword.toHex())){
                userVO.setVcode(NumberEnum.ZERO.getCode());
                return userVO;
            }
            //3.成功返回用户信息
            userVO.setUsername(teacher.getName());
            userVO.setPassword(teacher.getPassword());
            userVO.setRealname(teacher.getEngName());
            userVO.setStatus(new Integer(teacher.getStatus()));
            userVO.setUserRole(new Integer(RoleTypeEnum.TEACHER.getCode()));
            userVO.setId(teacher.getTeacherId());
        }else {
        // 其它角色
            User userDb = userMapper.selectUserByUsernameAndRole(user.getUsername(),user.getUserRole().toString());
            //1.判断系统用户是否为空，为空直接返回
            if(userDb==null){
                userVO.setVcode(NumberEnum.ZERO.getCode());
                return userVO;
            }
            // 如果用户状态为禁用 返回 提示用户账号已禁用 请联系管理员
            if(userDb.getStatus().toString().equals(NumberEnum.ZERO.getCode())){
                userVO.setStatus(userDb.getStatus());
                return userVO;
            }
            //2.判断参数密码和系统密码是否一直，不一致返回失败
            // 2.1用户密码加密的盐
            ByteSource salt = ByteSource.Util.bytes(user.getUsername() + user.getPassword());
            // 2.2加盐后的密码
            SimpleHash saltPassword = new SimpleHash("MD5", user.getPassword(), salt, 1024);
            if(!userDb.getPassword().equals(saltPassword.toHex())){
                userVO.setVcode(NumberEnum.ZERO.getCode());
                return userVO;
            }
            //3.成功返回用户信息
            userVO.setUsername(userDb.getUsername());
            userVO.setPassword(userDb.getPassword());
            userVO.setRealname(userDb.getRealname());
            userVO.setUserRole(userDb.getRoleType());
            userVO.setStatus(userDb.getStatus());
            userVO.setId(userDb.getId());
        }
            userVO.setVcode(NumberEnum.ONE.getCode());
        return userVO;
    }

    /**
     * 2.分页查询用户
     * @param params
     * @return
     */
    @Override
    public PageInfo<User> queryListByPage(SearchEntity params) {

        if(params.getPageNum() == null || params.getPageNum() == 0) {
            params.setPageNum(1);
        }
        if(params.getPageSize() == null || params.getPageSize() == 0) {
            params.setPageSize(10);
        }
        PageHelper.startPage(params.getPageNum(), params.getPageSize());
        List<User> list = userMapper.queryListByPage(params.getEntity());
        PageInfo<User> page = new PageInfo<User>(list);
        return page;
    }

    /**
     * 3.新增用户
     * @param user
     */
    @Override
    public void addUser(User user) {
        // 1.uuid
        user.setId(UUIDUtils.getPrimaryKey());
        // 2.创建时间
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        // 3.用户密码加密的盐
        ByteSource salt = ByteSource.Util.bytes(user.getUsername() + user.getPassword());
        // 4.加盐后的密码
        SimpleHash saltPassword = new SimpleHash("MD5", user.getPassword(), salt, 1024);
        // 5.设置盐
        user.setSalt(salt.toString());
        // 6.设置加盐后的密码
        user.setPassword(saltPassword.toString());
        userMapper.insert(user);
    }

    /**
     * 4.根据id查询用户
     * @param id
     * @return
     */
    @Override
    public User queryUserById(String id) {
        return userMapper.selectUserById(id);
    }

    /**
     * 5.更新用户
     * @param user
     */
    @Override
    public void updateUser(User user) {
        // 1.更新时间
        user.setUpdateTime(new Date());
        // 3.用户密码加密的盐
        ByteSource salt = ByteSource.Util.bytes(user.getUsername() + user.getPassword());
        // 4.加盐后的密码
        SimpleHash saltPassword = new SimpleHash("MD5", user.getPassword(), salt, 1024);
        // 5.设置盐
        user.setSalt(salt.toString());
        // 6.设置加盐后的密码
        user.setPassword(saltPassword.toString());
        System.out.println(user.toString());
        userMapper.updateUserById(user);
    }

    /**
     * 6.删除用户
     * @param id
     */
    @Override
    public void deleteUser(String id) {
        userMapper.deleteUserById(id);
    }

    /**
     * 7.根据用户名查询用户
     * @param username
     * @return
     */
    @Override
    public User findUserByUsername(String username) {
        return userMapper.selectUserByUsername(username);
    }

    /**
     * 8.更新密码
     * @param userDb
     */
    @Override
    public void updatePassword(User userDb) {
        userMapper.updateUserById(userDb);
    }
}
